In Python, is there a way to call a method on every item of an iterable? [closed]
Posted
by
Thane Brimhall
on Stack Overflow
See other posts from Stack Overflow
or by Thane Brimhall
Published on 2012-12-08T00:32:43Z
Indexed on
2012/12/09
23:04 UTC
Read the original article
Hit count: 156
Possible Duplicate:
Is there a map without result in python?
I often come to a situation in my programs when I want to quickly/efficiently call an in-place method on each of the items contained by an iterable. (Quickly meaning the overhead of a for loop is unacceptable). A good example would be a list of sprites when I want to call draw()
on each of the Sprite
objects.
I know I can do something like this:
[sprite.draw() for sprite in sprite_list]
But I feel like the list comprehension is misused since I'm not using the returned list. The same goes for the map
function. Stone me for premature optimization, but I also don't want the overhead of the return value.
What I want to know is if there's a method in Python that lets me do what I just explained, perhaps like the hypothetical function I suggest below:
do_all(sprite_list, draw)
© Stack Overflow or respective owner